﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HostFileChangerEnhanced
{
    
    public class ManageManagedDomainsConfiguration
    {

        private static HostFileManagedDomains managedDomainsData = new HostFileManagedDomains();

        public static HostFileManagedDomains.domainsToManageDataTable ManagedDomainsData
        {
            get { return managedDomainsData.domainsToManage; }
        }

        public static HostFileManagedDomains.domainOptionsRow[] GetDomainOptions(HostFileManagedDomains.domainsToManageRow domainRow)
        {
            return (HostFileManagedDomains.domainOptionsRow[])managedDomainsData.domainOptions.Select("c_DomainName = '" + domainRow.c_DomainName + "'");
        }

        private const string localPath = "managedDomains.xml";
        static ManageManagedDomainsConfiguration()
        {
            LoadManagedFile();
        }


        private static void LoadManagedFile()
        {
            #region Make sure file exists
            if (!File.Exists(localPath))
            {
                BuildNewManagedFile();
            }
            else
            {

                try
                {
                    managedDomainsData.ReadXml(localPath);
                }
                catch
                {

                    BuildNewManagedFile();
                }
            }
            #endregion
        }

        private static void BuildNewManagedFile()
        {

            HostFileManagedDomains.domainsToManageRow exampleDomainRow = managedDomainsData.domainsToManage.NewdomainsToManageRow();
            exampleDomainRow.c_PriorityOrder = 1;
            exampleDomainRow.c_DomainName = "www.sevder.com";
            managedDomainsData.domainsToManage.Rows.Add(exampleDomainRow);

            HostFileManagedDomains.domainOptionsRow exampleDomainOptionsRow = managedDomainsData.domainOptions.NewdomainOptionsRow();
            exampleDomainOptionsRow.c_DomainName = "www.sevder.com";
            exampleDomainOptionsRow.c_AlternateName = "Development";
            exampleDomainOptionsRow.c_AlternateIp = "127.0.0.1";
            managedDomainsData.domainOptions.Rows.Add(exampleDomainOptionsRow);

            managedDomainsData.WriteXml(localPath);

        }

        /// <summary>
        /// Adds new setting to the local xml database
        /// </summary>
        /// <param name="domainName">domain name to add</param>
        /// <param name="domainAlternateName">alternate name to manage personally</param>
        /// <param name="alternateIp">alternate ip address of the domain name</param>
        public static void AddNewDomainSetting(string domainName, string domainAlternateName, string alternateIp)
        {
            HostFileManagedDomains.domainsToManageRow foundDomainRow = managedDomainsData.domainsToManage.FindByc_DomainName(domainName);
            if (foundDomainRow == null)
            {
                HostFileManagedDomains.domainsToManageRow anotherDomainRow = managedDomainsData.domainsToManage.NewdomainsToManageRow();
                anotherDomainRow.c_PriorityOrder = 1;
                anotherDomainRow.c_DomainName = domainName;
                managedDomainsData.domainsToManage.Rows.Add(anotherDomainRow);
            }

            HostFileManagedDomains.domainOptionsRow foundDomainOptionsRow = managedDomainsData.domainOptions.FindByc_DomainNamec_AlternateName(domainName, domainAlternateName);
            if (foundDomainOptionsRow == null)
            {
                HostFileManagedDomains.domainOptionsRow anotherDomainOptionsRow = managedDomainsData.domainOptions.NewdomainOptionsRow();
                anotherDomainOptionsRow.c_DomainName = domainName;
                anotherDomainOptionsRow.c_AlternateName = domainAlternateName;
                anotherDomainOptionsRow.c_AlternateIp = alternateIp;
                managedDomainsData.domainOptions.Rows.Add(anotherDomainOptionsRow);
            }

            //write final settings
            managedDomainsData.WriteXml(localPath);
        }

        /// <summary>
        /// removes a setting from the locally hosted xml database
        /// </summary>
        /// <param name="listBoxView">"domainname - alternatename - ipaddress" formatted string</param>
        public static void RemoveDomainSetting(string listBoxView)
        {
            string[] displayParts = listBoxView.Replace(" - ", "-").Split('-');
            string domainName = displayParts[0];
            string domainAlternateName = displayParts[1];
            HostFileManagedDomains.domainOptionsRow foundDomainOptionRow = managedDomainsData.domainOptions.FindByc_DomainNamec_AlternateName(domainName, domainAlternateName);
            if (foundDomainOptionRow != null)
            {
                managedDomainsData.domainOptions.Rows.Remove(foundDomainOptionRow);
            }

            HostFileManagedDomains.domainOptionsRow[] otherDomainOptionRows = managedDomainsData.domainOptions.Select(managedDomainsData.domainOptions.c_DomainNameColumn.ColumnName + "='" + domainName + "'") as HostFileManagedDomains.domainOptionsRow[];

            if (otherDomainOptionRows == null || otherDomainOptionRows.Length == 0)
            { 
                //no more option rows left so we must remove from domain list also
                HostFileManagedDomains.domainsToManageRow foundDomainRow = managedDomainsData.domainsToManage.FindByc_DomainName(domainName);
                if (foundDomainRow != null)
                {
                    managedDomainsData.domainsToManage.Rows.Remove(foundDomainRow);
                }
            }

            //write final settings
            managedDomainsData.WriteXml(localPath);
        }

    }
}
